// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   FavoriteDaoImpl.java

package com.itheima.travel.dao.impl;

import com.itheima.travel.dao.IFavoriteDao;
import com.itheima.travel.model.*;
import com.itheima.travel.util.JdbcUtils;
import java.sql.SQLException;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class FavoriteDaoImpl
	implements IFavoriteDao
{

	private JdbcTemplate jdbcTemplate;

	public FavoriteDaoImpl()
	{
		jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());
	}

	public int updateRouteFavoriteNum(JdbcTemplate jdbcTemplate, int rid)
		throws SQLException
	{
		String sql = "update tab_route set count=count+1 where rid=?";
		return jdbcTemplate.update(sql, new Object[] {
			Integer.valueOf(rid)
		});
	}

	public int getCountByUid(int uid)
		throws SQLException
	{
		String sql = "SELECT COUNT(*) FROM tab_favorite WHERE uid=?";
		return ((Integer)jdbcTemplate.queryForObject(sql, new Object[] {
			Integer.valueOf(uid)
		}, java/lang/Integer)).intValue();
	}

	public List findFavoriteListByPage(int uid, int curPage, int pageSize)
		throws SQLException
	{
		String sql = "SELECT * FROM tab_favorite f,tab_route r WHERE f.rid=r.rid AND f.uid=? ORDER BY f.date DESC LIMIT ?,?";
		return jdbcTemplate.queryForList(sql, new Object[] {
			Integer.valueOf(uid), Integer.valueOf((curPage - 1) * pageSize), Integer.valueOf(pageSize)
		});
		DataAccessException e;
		e;
		e.printStackTrace();
		return null;
	}

	public int addFavorite(JdbcTemplate jdbcTemplate, Favorite favorite)
		throws SQLException
	{
		String sql = "insert into tab_favorite values(?,?,?)";
		return jdbcTemplate.update(sql, new Object[] {
			Integer.valueOf(favorite.getRoute().getRid()), favorite.getDate(), Integer.valueOf(favorite.getUser().getUid())
		});
	}

	public Favorite findFavoriteByRidAndUserId(int rid, int uid)
		throws SQLException
	{
		String sql = "select * from tab_favorite where rid=? and uid=?";
		return (Favorite)jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(com/itheima/travel/model/Favorite), new Object[] {
			Integer.valueOf(rid), Integer.valueOf(uid)
		});
		DataAccessException e;
		e;
		e.printStackTrace();
		return null;
	}
}
